package com.chookss.home.audit;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chookss.R;
import com.chookss.Urls;
import com.chookss.home.audit.adapter.ClockInProcessAdapter;
import com.chookss.home.entity.ProcessEntity;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.google.gson.reflect.TypeToken;
import com.lvgroup.hospital.base.BaseAct;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

public class ApprovalProcessActivity extends BaseAct {
//审批流程明细列表   补卡  请假  报销  物品等等的流程

    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.llBg)
    LinearLayout llBg;
    @BindView(R.id.line)
    View line;
    @BindView(R.id.rvView)
    RecyclerView recyclerView;
    @BindView(R.id.srlView)
    SmartRefreshLayout smrRf;
    private ClockInProcessAdapter adapter;
    private List<ProcessEntity> list = new ArrayList<>();
    private int currentPage = 1;
    private int pageSize = 20;
    private boolean isEnd = false;//是否到底
    private View emptyView;
    private TextView tvNone;//空布局;
    private int type = 3;//  // 1-新闻 2-视频 3-补卡 4-请假 5-外勤 6-图文7报销8物料9借款10补贴11用章12维修13项目交付14离职 15晋升16 回款17 修改项目金额18入职19加班
    private String projectCode,shangbanCd;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_srlview_top);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();
        type = getIntent().getIntExtra("type", 7);
        projectCode = getIntent().getStringExtra("projectCode");
        shangbanCd = getIntent().getStringExtra("shangbanCd");
        commonTitleTxt.setText("审批记录");
        line.setVisibility(View.VISIBLE);
        llBg.setBackgroundColor(getResources().getColor(R.color.white));
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) smrRf.getLayoutParams();
        params.setMargins(0, Utils.dip2px(this, 12), 0, 0);
        smrRf.setLayoutParams(params);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setPadding(Utils.dip2px(this, 13),0 , Utils.dip2px(this, 13), 0);
        adapter = new ClockInProcessAdapter(R.layout.item_clock_in_process, this, list,type);
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        recyclerView.setAdapter(adapter);
        emptyView = LayoutInflater.from(this).inflate(R.layout.layout_common_empty, recyclerView, false);
        tvNone = emptyView.findViewById(R.id.tvEmpty);
        tvNone.setText("暂无记录");
        smrRf.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (isEnd) {//has next page
                    MyToast.show("没有更多数据了");
                    loadEnd();
                } else {
                    currentPage++;
                    getData();
                }
            }
        });
        smrRf.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                isEnd = false;
                currentPage = 1;
                getData();
            }
        });
        smrRf.autoRefresh();
    }
    private void getData() {
        //审批基本数据
        Map<String, String> map = new HashMap<>();
        map.put("projectCode",projectCode);
        map.put("type", type+"");
        if(type==3||type==5){
            map.put("shangbanCd",shangbanCd);
        }
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<List<ProcessEntity>>>() {
        }.getType(), null, Urls.selectAuditDetailList, SignUtils2.makeSignStr(map), false, new ObserverCallback<List<ProcessEntity>>() {
            @Override
            public void onSuccess(List<ProcessEntity> data) {
                loadEnd();
                if (smrRf == null) {
                    return;
                }
                list.clear();
                String name = "",headUrl="";
                for (int i = 0; i <data.size() ; i++) {
                    list.add(data.get(i));
                    if(data.get(i).getConnectType().equals("0")){
                        if (!Utils.isNull(data.get(i).getRealAuditEmployeeName())) {
                            name=data.get(i).getRealAuditEmployeeName();
                        }else{
                            name=data.get(i).getCreateEmployeeName();
                        }
                        if (!Utils.isNull(data.get(i).getRealAuditHeadPhotoThumbUrl())) {
                            headUrl=data.get(i).getRealAuditHeadPhotoThumbUrl();
                        }else{
                            headUrl=data.get(i).getHeadPhotoThumbUrl();
                        }
                    }
                    if(data.get(i).getEndStep().equals("1")){
                        ProcessEntity endProcessEntity  = new ProcessEntity();
                        if(Utils.isNull(name)){
                            endProcessEntity.setCreateEmployeeName(data.get(0).getCreateEmployeeName());
                            endProcessEntity.setRealAuditEmployeeName(data.get(0).getCreateEmployeeName());
                        }else{
                            endProcessEntity.setCreateEmployeeName(name);
                            endProcessEntity.setRealAuditEmployeeName(name);
                        }
                        if(Utils.isNull(headUrl)){
                            endProcessEntity.setHeadPhotoThumbUrl(data.get(0).getHeadPhotoThumbUrl());
                        }else{
                            endProcessEntity.setHeadPhotoThumbUrl(headUrl);
                        }
                        endProcessEntity.setConnectType("0");
                        endProcessEntity.setEndProcess(true);
                        endProcessEntity.setAuditStatus("1");
//                        if(i>0){
//                            endProcessEntity.setAuditTime(data.get(i).getAuditTime());
//                        }
                        list.add(endProcessEntity);
                    }
                }
                adapter.notifyDataSetChanged();

            }

            @Override
            public void onError(String errorMsg) {
                loadEnd();
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }
    private void loadEnd() {
        if (null != smrRf && smrRf.getState() == RefreshState.Refreshing)
            smrRf.finishRefresh(true);
        if (null != smrRf && smrRf.getState() == RefreshState.Loading)
            smrRf.finishLoadMore(true);
    }

    @OnClick(R.id.common_title_back)
    public void onClick() {
        finish();
    }
}
